{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "98fcf438",
   "metadata": {},
   "source": [
    "切片\n",
    "想从一个序列中获取一个以上的元素,就需要使用切片语法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a06bddfd",
   "metadata": {},
   "source": [
    "sequence[start:stop:step]\n",
    "该区间只包含start不包含stop,step决定切片的步长与方向"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4740f2f2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'PYT'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "language = \"PYTHON\"\n",
    "language[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2b333605",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'YT'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "language[1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4eb4fc3e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'HON'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "language[-3:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "192ad256",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'HO'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "language[-3:-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e66f8424",
   "metadata": {},
   "source": [
    "列表\n",
    "\n",
    "可以存储多个不同数据类型的多个对象"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e165632",
   "metadata": {},
   "source": [
    "语法:\n",
    "[element1,element2,element3,...]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8aa25aaa",
   "metadata": {},
   "outputs": [],
   "source": [
    "file_names = [\"one.xlsx\",\"two.xlsx\",\"three.xlsx\"]\n",
    "number = [1,2,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a889feb8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['one.xlsx', 'two.xlsx', 'three.xlsx', 1, 2, 3]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "file_names + number"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12fc15f2",
   "metadata": {},
   "source": [
    "嵌套列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a7dfaacc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4, 5, 6]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nestd_list = [[1,2,3],\n",
    "              [4,5,6],\n",
    "              [7,8,9]]\n",
    "nestd_list[1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efa38354",
   "metadata": {},
   "source": [
    "更改列表中的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "26816470",
   "metadata": {},
   "outputs": [],
   "source": [
    "user = [\"Linda\",\"Brian\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "c8eb1245",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jennifer', 'Javen', 'Sunny', 'Linnet']"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user.append(\"Jennifer\")#在末尾追加元素\n",
    "user.append(\"Javen\")\n",
    "user.append(\"Sunny\")\n",
    "user.append(\"Linnet\")\n",
    "user"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "d7ead0f4",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Kim', 'Jennifer', 'Javen', 'Sunny', 'Linnet']"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user.insert(0,\"Kim\")#在 索引0处插入元素\n",
    "user"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb367181",
   "metadata": {},
   "source": [
    "删除元素:\n",
    "默认使用pop或者del,pop是一个方法,del是一个python语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "6bafa6bf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Linnet'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user.pop() #默认情况下,删除并且返回最后一个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "e12bac42",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Kim', 'Jennifer', 'Javen', 'Sunny']"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "b9308d6e",
   "metadata": {},
   "outputs": [],
   "source": [
    "del user[0] #默认情况下,移除索引处的元素"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd69b796",
   "metadata": {},
   "source": [
    "求长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "e458eb9b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(user)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b78daa17",
   "metadata": {},
   "source": [
    "检查列表是否包含某个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "1fb21ebc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"Linnet\" in user"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "5c810f26",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"Sunny\" in user"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5621b2be",
   "metadata": {},
   "source": [
    "排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "69bddd0a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Javen', 'Jennifer', 'Sunny']\n",
      "['Jennifer', 'Javen', 'Sunny']\n"
     ]
    }
   ],
   "source": [
    "print(sorted(user))\n",
    "print(user)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4be37c1d",
   "metadata": {},
   "source": [
    "对原列表排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "dc31a3cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Javen', 'Jennifer', 'Sunny']"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user.sort()\n",
    "user"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4e38b49",
   "metadata": {},
   "source": [
    "集合"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b750a206",
   "metadata": {},
   "source": [
    "集合是一种没有重复元素的集合,在实践中它们常被用于列表去重或者元素去重"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e958d015",
   "metadata": {},
   "source": [
    "语法:\n",
    "用花括号创建集合->{element1,element2,element3,...}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9caf605d",
   "metadata": {},
   "source": [
    "对列表和元组进行去重:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "884f9e2d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'EUR', 'SGD', 'USD'}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set([\"USD\", \"USD\", \"SGD\", \"EUR\", \"USD\", \"EUR\"])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:xl38] *",
   "language": "python",
   "name": "conda-env-xl38-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
